<template>
    <div class="main">
        <div class="box">
            <div class="search">
                <div class="letters">
                    <router-link :to="'/singer?letter=' + letter + '&area=' + area + '&gender=' + gender + '&pageNo=' + 1">
                        <span class="all" @click="search($event)" :class="{active: letter==''}" data-name='字母'>全部</span>
                    </router-link>
                    <router-link  v-for="(l,i) in letters" :key="i" :to="'/singer?letter=' + letter + '&area=' + area + '&gender=' + gender + '&pageNo=' + 1">
                        <span :class="{active: letter==l}" @click="search($event)" :data-letter='l' data-name='字母'>{{l}}</span>
                    </router-link>
                </div>
                <div class="areas">
                    <router-link :to="'/singer?letter=' + letter + '&area=' + area + '&gender=' + gender + '&pageNo=' + 1">
                        <span @click='search($event)' class="all" :class="{active: area==''}" data-name='区域'>全部</span>
                    </router-link>
                    <router-link :to="'/singer?letter=' + letter + '&area=' + area + '&gender=' + gender + '&pageNo=' + 1">
                        <span v-for="(l,i) in areas" :key="i" @click='search($event)' :class="{active: area==l}" :data-area='l' data-name='区域'>{{l}}</span>
                    </router-link>
                </div>
                <div class="other">
                    <router-link :to="'/singer?letter=' + letter + '&area=' + area + '&gender=' + gender + '&pageNo=' + 1">
                        <span @click='search($event)' class="all" :class="{active: gender==''}" data-name='其他'>全部</span>
                    </router-link>
                    <router-link :to="'/singer?letter=' + letter + '&area=' + area + '&gender=' + gender + '&pageNo=' + 1">
                        <span v-for="(l,i) in genders" :key="i" @click='search($event)' :class="{active: gender==l}" :data-gender='l' data-name='其他'>{{l}}</span>
                    </router-link>
                </div>
            </div>
            <div class="infos">
                <div class="singer" v-for="(s,i) in singers" :key="i" v-show="i<12">
                    <router-link :to="'/singer/' + s.id">
                    <div class="pic">
                        <img v-lazy="s.picUrl" alt="">
                    </div>
                    <div class="name">{{s.name}}</div>
                    </router-link>
                </div>
            </div>
            <div class="moreSinger">  
                <span v-for="(s,i) in singers" :key="i" v-show="i>=12">
                    <router-link :to="'/singer/' + s.id">{{s.name}}</router-link>
                </span>     
            </div>         
            <el-pagination background layout="prev, pager, next" 
            :page-size="pageSize" :total="total - 0" :current-page="$route.query.pageNo - 0"
            @current-change="changePage">
                    
            </el-pagination>          
        </div>
    </div>
</template>

<script>

export default {
    data() {
        return {
            letters: ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","#",],
            areas: ['内地','欧美','韩国','日本','其他'],
            genders: ['男','女','乐队'],
            letter: '',
            area: '',
            gender: '',
            total: '',
            pageSize: 48,
            page: 1,
            singers: [],
            index: null,
            loading: false
        }
    },
    created() {
        this.letter = this.$route.query.letter || ''
        this.area = this.$route.query.area || ''
        this.gender = this.$route.query.gender || ''
        this.page = this.$route.query.pageNo || 1

        this.getSinger(this.letter,this.area,this.gender,this.page)
    },
    beforeRouteUpdate (to, from, next) {
        this.getSinger(to.query.letter,to.query.area,to.query.gender,to.query.pageNo)
        next()
    },
    methods: {
        changePage(page){
            this.$router.push(`/singer/?letter=${this.letter}&area=${this.area}&gender=${this.gender}&pageNo=${page}`)
        },
        search(ev){
            if(ev.target.dataset.name=='字母') this.letter = ev.target.dataset.letter || ''
            if(ev.target.dataset.name=='区域') this.area = ev.target.dataset.area || ''
            if(ev.target.dataset.name=='其他') this.gender = ev.target.dataset.gender || ''
            this.getSinger(this.letter,this.area,this.gender,this.$route.query.pageNo)
        },
        pager(page){
            this.page = page
            this.getSinger()
        },
        getSinger(letter,area,gender,pageNo){   
            let urlPageNo = (pageNo-1) * 48
            let urlArea = -1
            let urlType = -1
            switch (area) {
                case '内地':
                    urlArea = 7
                    break;
                case '内地':
                    urlArea = 7
                    break;
                case '欧美':
                    urlArea = 96
                    break;
                case '韩国':
                    urlArea = 16
                    break;
                case '日本':
                    urlArea = 8
                    break;
                case '其他':
                    urlArea = 0
                    break;
                default:
                    urlArea = -1
                    break;
            }
            switch (gender) {
                case '男':
                    urlType = 1
                    break;
                case '女':
                    urlType = 2
                    break;
                case '乐队':
                    urlType = 3
                    break;
                default:
                    urlType = -1
                    break;
            }
            var url = `https://autumnfish.cn/artist/list?type=${urlType}&area=${urlArea}&initial=${letter}&limit=48&offset=${urlPageNo}`
            this.axios.get(url).then((res) => {
                // console.log(res.data.artists);
                this.total =4700
                this.singers = res.data.artists
             
            })
        }
    },
}
</script>

<style scoped>
    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    .search,.infos,.moreSinger{
        min-width: 1228px;
    }
    a{
        text-decoration: none;
        color: #222;
    }
    .box{
        width: 80vw;
        margin: 0 auto;
    }
    .search,.search div,.infos,.infos .singer{
        display: flex;
        /* justify-content: space-between; */
        flex-wrap: wrap;
    }
    .search div{
        align-items: center;
        margin-top: 15px;
    }
    .search{
        margin-top: 30px;
        flex-direction: column;
    }
    .letters{
        width: 100%;
    }
    .areas{
        width: 37%;
    }
    .other{
        width: 25%;
    }
    .search .all{      
        padding: 6px 16px;
        border-radius: 15px;
    }
    .search span{
        padding: 6px 16px;
        border-radius: 15px;
        cursor: pointer;
    }
    .search span.active{
        background-color: red;
        color: #fff;
    }
    .infos .singer{
        flex-direction: column;
        width: 16%;
        justify-content: center;
        padding: 20px 0;
        cursor: pointer;
    }
    .infos .singer .name{
        text-align: center;
    }
    .infos .pic{
        display: flex;
        justify-content: center;
        align-items: center;
        width: 180px;
        height: 180px;
        overflow: hidden;
    }
    .infos img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
    }
    .infos img:hover{
        transform: scale(1.02);
        transition: all .6s;
    }
    .moreSinger{
        width: 80vw;
        margin: 0 auto;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
    }
    .moreSinger span{
        width: 16%;
        margin: 10px 0;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    .el-pagination{
        display: flex;
        justify-content: center;
        padding-top: 50px;
    }

    
</style>